#! /usr/bin/python
from HueConf import KEY, HUB_IP
from libs.utils import color, put, on, off
from libs.NewHueScript import HueScript, TT_SEC
from libs.HueBridge import HueBridge
from libs.HueCue import HueCue

class SunriseScript(HueScript):
    def __init__(self, *posargs, **kwargs):
        self.initial_state = {
            'r': 80,
            'b': 255,
            'datadict': {
                'bri': 0
            }
        }

        HueScript.__init__(self, *posargs, **kwargs)


    def makeCues(self):
        cues = []
        lamps = self.bridge.lamps

        # make sure everything is off
        for l in lamps:
            cues.append( HueCue(0, l.setColor, kwargs=self.initial_state) )
            cues.append( HueCue(0, l.off) )
#        self.register(0, color, [[1,2,3]], self.initial_state)
#        self.register(0, off, [[1,2,3]] )

        # turn on the lights
        self.initial_state['datadict'].update({'on': True})
 
        cues.append( HueCue(2, lamps[0].setColor, kwargs=self.initial_state) )
        cues.append( HueCue(602, lamps[2].setColor, kwargs=self.initial_state) )
        cues.append( HueCue(902, lamps[1].setColor, kwargs=self.initial_state) )
#        self.register(2, color, 1, self.initial_state)
#        self.register(600, color, 3, self.initial_state)
#        self.register(300, color, 2, self.initial_state)

        # start the sunrise

        cues.append( HueCue(1142, lamps[1].setattr_,
                    [ 'ct', (497, {'bri': 0, 'transitiontime': 600 * TT_SEC}) ]
                ))
#        self.register(240, put, 2, {'data': {
#                'ct': 497,
#                'bri': 0,
#                'transitiontime': 600 * TT_SEC
#            }
#        })

        for idx, l in enumerate(lamps):
            if idx == 1:
                continue

            cues.append( HueCue(1232, l.setColor, kwargs={
                    'r': 254,
                    'g': 133,
                    'b': 0,
                    'datadict': {
                        'bri': 90,
                        'transitiontime': 480 * TT_SEC
                    }
                }
            ))
#        self.register(90, color, [[1, 3]], {
#                'r': 254,
#                'g': 133,
#                'b': 0,
#                'data': {
#                    'bri': 90,
#                    'transitiontime': 480 * TT_SEC
#                }
#            }
#        )

        cues.append( HueCue(1742, lamps[1].setColor, kwargs={
                'r': 150,
                'g': 254,
                'b': 0,
                'datadict': {
                    'bri': 255,
                    'transitiontime': 480 * TT_SEC
                }
            }
        ))
#        self.register(510, color, 2, {
#                'r': 150,
#                'g': 254,
#                'b': 0,
#                'data': {
#                    'bri':254,
#                    'transitiontime': 420 * TT_SEC
#                }
#        })

        for idx, l in enumerate(lamps):
            if idx == 1:
                cues.append( HueCue(2142, l.setattr_, [
                        'ct', (350, {'bri': 190,'transitiontime': 300 * TT_SEC})
                    ]
                ))
            else:
                cues.append( HueCue(2142, l.setattr_, [
                        'ct', (440, {'bri': 140,'transitiontime': 230 * TT_SEC})
                    ]
                ))

#        self.register(400, put, [[1,3]], {'data': {
#                'ct': 440,
#                'bri': 140,
#                'transitiontime': 230 * TT_SEC
#            }
#        })
#        self.register(0, put, 2, {'data': {
#                'ct': 350, 
#                'bri': 190,
#                'transitiontime': 300 * TT_SEC
#            }
#        })

        for idx, l in enumerate(lamps):
            if idx == 1:
                continue
            cues.append( HueCue(2372, l.setattr_, [
                        'ct', (370, {'bri': 200,'transitiontime': 360 * TT_SEC})
                    ]
                ))

#        self.register(230, put, [[1,3]], {'data': {
#                'ct': 370,
#                'bri': 200,
#                'transitiontime': 360 * TT_SEC
#            }
#        })

        cues.append( HueCue(2472, lamps[1].setattr_, [
                        'ct', (320, {'bri': 240,'transitiontime': 360 * TT_SEC})
                    ]
                ))
#        self.register(100, put, 2, {'data': {
#                'ct': 320,
#                'bri': 240,
#                'transitiontime': 360 * TT_SEC
#            }
#        })

        for l in lamps:
            cues.append( HueCue(2732, l.setattr_, [
                        'ct', (290, {'bri': 255,'transitiontime': 900 * TT_SEC})
                    ]
                ))
#        self.register(360, put, [[1,2,3]], {'data': {
#                'ct': 290,
#                'bri': 254,
#                'transitiontime': 900 * TT_SEC
#            }
#        })

        self.cues = cues

if __name__ == '__main__':
    sunrise = SunriseScript(bridge=HueBridge(ipaddr=HUB_IP, key=KEY))
    sunrise()
